﻿using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Module.Extend.EWRA.Attributes;
using EFFC.Frame.Net.Module.Extend.EWRA.Attributes.Validation;
using ICPFrameLib.Attributes;
using ICPFrameLib.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFFC.Extends.LinqDLR2SQL;
using EFFC.Frame.Net.Unit.DB;

namespace XHRPMIS.Business.v1._0
{
    public class SSO : MyRestLogic
{
    [EWRARoute("patch", "/sso/register")]
    [EWRARouteDesc("给当前登录账号做注册")]
    [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息"",
login_uid:""成功时候返回的登录账号""
}")]
    public object SyncSSOAccount()
    {
        string loginuid = ComFunc.nvl(TokenPayLoad["Login_UID"]);
        string loginid = TokenPayLoad.ID;
        string loginpass = ComFunc.nvl(TokenPayLoad["pw"]);
        string loginname = ComFunc.nvl(TokenPayLoad["name"]);
        string usertype = ComFunc.nvl(TokenPayLoad["usertype"]);
        string userno = ComFunc.nvl(TokenPayLoad["userno"]);
        string mobile = ComFunc.nvl(TokenPayLoad["mobile"]);
        string qq = ComFunc.nvl(TokenPayLoad["qq"]);
        string avatar = ComFunc.nvl(TokenPayLoad["avatar"]);
        string roleno = ComFunc.nvl(TokenPayLoad["roleno"]);

        BeginTrans();
        var up = DB.NewMetaUnitParameter();
        dynamic existsinfo = (from t in DB.LamdaTable(up, "EXTEND_LOGIN", "a")
                              where t.loginid == loginid
                              select t).GetQueryList(up).FirstOrDefault();
        if (existsinfo != null)
        {
            if (existsinfo.UID != loginuid)
            {
                //修改登录名为{原登录名}_bak
                DB.QuickUpdate(up, "EXTEND_LOGIN", new
                {
                    LoginID = $"{existsinfo.LoginID}_bak",
                    last_id = TokenPayLoad.ID,
                    last_ip = ClientInfo.IP,
                    last_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                }, new
                {
                    existsinfo.UID
                });
                //新增新的登录资料
                DB.QuickInsert(up, "EXTEND_LOGIN", new
                {
                    UID = loginuid,
                    LoginID = loginid,
                    UserNo = userno,
                    LoginName = loginname,
                    UserType = usertype,
                    LoginPass = loginpass,
                    Mobile = mobile,
                    QQ = qq,
                    Avatar = avatar,
                    PlatformID = loginid,
                    add_id = TokenPayLoad.ID,
                    add_ip = ClientInfo.IP,
                    add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                });
                var roleslist = (from t in DB.LamdaTable(up, "EXTEND_ROLE_LOGIN", "a")
                                 where t.LoginUID == existsinfo.UID
                                 select new
                                 {
                                     t.RoleNo,
                                     LoginUID = loginuid,
                                     add_id = TokenPayLoad.ID,
                                     add_ip = ClientInfo.IP,
                                     add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                                 }).GetQueryList(up);
                //将原账户的角色权限复制过来
                foreach (var item in roleslist)
                {
                    DB.QuickInsert(up, "EXTEND_ROLE_LOGIN", item);
                }
            }
            else
            {
                //更新个人登录账号信息
                DB.QuickUpdate(up, "EXTEND_LOGIN", new
                {
                    LoginID = loginid,
                    UserNo = userno,
                    LoginName = loginname,
                    UserType = usertype,
                    LoginPass = loginpass,
                    Mobile = mobile,
                    QQ = qq,
                    Avatar = avatar,
                    PlatformID = loginid,
                    last_id = TokenPayLoad.ID,
                    last_ip = ClientInfo.IP,
                    last_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                }, new
                {
                    UID = loginuid
                });
            }
        }
        else
        {
            DB.QuickInsert(up, "EXTEND_LOGIN", new
            {
                UID = loginuid,
                LoginID = loginid,
                UserNo = userno,
                LoginName = loginname,
                UserType = usertype,
                LoginPass = loginpass,
                Mobile = mobile,
                QQ = qq,
                Avatar = avatar,
                PlatformID = loginid,
                add_id = TokenPayLoad.ID,
                add_ip = ClientInfo.IP,
                add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                last_id = TokenPayLoad.ID,
                last_ip = ClientInfo.IP,
                last_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

            });
            //新增角色资料，只提供R999和R900，其它的角色权限由各个系统自己设置
            foreach (var r in roleno.Split(",", StringSplitOptions.RemoveEmptyEntries))
            {
                if (r != "R999" && r != "R900") continue;
                DB.QuickInsert(up, "EXTEND_ROLE_LOGIN", new
                {
                    RoleNo = r,
                    LoginUID = loginuid,
                    add_id = TokenPayLoad.ID,
                    add_ip = ClientInfo.IP,
                    add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                });
            }
        }

        CommitTrans();
        return new
        {
            code = "success",
            msg = "新增成功",
            login_uid = loginuid
        };
    }
}
}
